﻿using ConfExpert.Forms.Base;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevTeam;


using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.SatinAlma
{
    public partial class _UnspedEsle : XForm
    {
        public _UnspedEsle()
        {
            InitializeComponent();
        }

        private void UnspedEsle_Load(object sender, EventArgs e)
        {

            /*
            UnspedWS.KarbelDataWsdlSoapClient unsped = new UnspedWS.KarbelDataWsdlSoapClient();
            string userName = Session.UnspedUsername;
            string password = Session.UnspedPassword;

            string xml = unsped.AcilanIthalatDosyalariniAl(userName, password);

            System.Data.DataSet dsAcilan = new DataSet();
            System.IO.StringReader xmlSR = new System.IO.StringReader(xml);
            dsAcilan.ReadXml(xmlSR);

            //xml = unsped.IthalatVerisiniAl(userName, password, "UNSPED_DOSYA_NO");
            List<string> silinen = new List<string>();

            DataTable dt = dsAcilan.Tables[0];
            if (!dt.Rows[0][0].ToStr().StartsWith("Karbel") &&   //Karbele ait açılmış herhangi bir dosya bulunamadı
                !dt.Rows[0][0].ToStr().Contains("Timeout expired"))
            {
                Notify(dt.Rows.Count + " yeni kayıt alındı.");
                dt.PrimaryKey = new DataColumn[] { dt.Columns["UNSPED_DOSYA_NO"] };
                DataTable dtYeni = dt.Copy();

                DataTable dtHepsi = ExecuteDataTable("UpdUnsped_Acilan", dt.Serialize());
                foreach (DataRow dr in dtHepsi.Rows)
                {
                    //this.Invoke(new MethodInvoker(() => Notify(dr["UNSPED_DOSYA_NO"].ToString())));

                    xml = unsped.IthalatVerisiniAl(userName, password, dr["UNSPED_DOSYA_NO"].ToString());

                    if (xml.Contains("herhangi bir veri bulunamadı"))
                        silinen.Add(dr["UNSPED_DOSYA_NO"].ToString());
                    else
                    {
                        System.Data.DataSet dsIthalatVerisi = new DataSet();
                        xmlSR = new System.IO.StringReader(xml);
                        dsIthalatVerisi.ReadXml(xmlSR);

                        DataTable dti = dsIthalatVerisi.Tables[0];
                        dti.PrimaryKey = new DataColumn[] { dti.Columns["UNSPED_DOSYA_NO"] };

                        dsAcilan.Merge(dsIthalatVerisi);
                        //dt.Rows.Find(dr["UNSPED_DOSYA_NO"])["TUR2"] = dsIthalatVerisi.Tables[0].Rows[0]["TUR"].ToString();
                    }
                }

                ExecuteNonQuery("UpdUnsped_Acilan", dsAcilan.Tables[0].Serialize());

                string AldimResult = string.Empty;
                foreach (DataRow dr in dtYeni.Rows)
                    AldimResult = unsped.AcilanIthalatDosyaniAldim(userName, password, dr["UNSPED_DOSYA_NO"].ToStr()); //Web servisinden fırlayan exception : Timeout -> SQL Exception
            }

            if (silinen.Count > 0)
                ExecuteNonQuery("UpdUnsped_Silinen", string.Join(",", silinen.ToArray()));
            */

            beiYontem.EditValue = Yontem = "FaturaToplamı";
            beiYontem.EditValueChanged += beiYontem_EditValueChanged;

            gridView1.Init("GetUnsped_Acilan2", Yontem);
            gridView1.AddDetailGrid(gridView2, "UNSPED_DOSYA_NO");
            gridView1.MakeColumnInvisible("TUR");
            gridView1.YanYanaGetir("MUSTERI:FATURA_NO");
            gridView2.MakeColumnInvisible("UNSPED_DOSYA_NO");
            gridView2.FormatAsDate("InvoiceTarihi");
            gridView2.FormatAsNumber(1, "LineTotals", "FaturaToplami", "Fark");
            splitContainerControl1.AutoSize();

            gridView1.MakeRowSelectable();
            //gridView2.MakeRowSelectable();

            gridView1.FormatAsNumber(2, "FATURA_TUTARI", "MIKTAR", "BRUTKG");
            gridView1.MakeColumnInvisible("FIRMA");
            gridView1.AddSummary("MIKTAR", "BRUTKG");
            gridView1.RowCellStyle += gridView1_RowCellStyle;

            //where FIRMA like 'KARBEL%'!


            gridView1.ActiveFilterString = "AlisFaturasiId = ''";
            bbiEsle.ItemClick += bbiEsle_ItemClick;
            gridView2.DoubleClick += gridView2_DoubleClick;
            gridView1.MakeReadOnly();
            gridView2.MakeReadOnly();
            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
        }

        void gridView2_DoubleClick(object sender, EventArgs e)
        {
            GridView view = (GridView)sender;
            Point pt = view.GridControl.PointToClient(Control.MousePosition);
            GridHitInfo info = view.CalcHitInfo(pt);
            if (info.RowHandle > -1)
            {
                int AFid = view.GetFocusedRowCellValue("AlisFaturasiId").ToInt();
                gridView1.SetFocusedRowCellValue("AlisFaturasiId", AFid);
            }
            //string colCaption = info.Column == null ? "N/A" : info.Column.GetCaption();
            view.EndEditing();
        }

        string Yontem = string.Empty;
        void beiYontem_EditValueChanged(object sender, EventArgs e)
        {
            if (beiYontem.EditValue != null)
            {
                Yontem = beiYontem.EditValue.ToString();
                gridView1.RefreshDataSource(true, "GetUnsped_Acilan2", Yontem);
                gridView1.BestFitColumns();
            }
        }


        void bbiEsle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            DataTable esle = new DataTable("Esle");
            esle.Columns.Add("UnspedId", typeof(int));
            esle.Columns.Add("AlisFaturasiId", typeof(int));

            foreach (DataRow dr in gridView1.DataTable().Rows)
                if (dr["UnspedId"].ToInt() > 0 && dr["AlisFaturasiId"].ToInt() > 0)
                    esle.Rows.Add(dr["UnspedId"].ToInt(), dr["AlisFaturasiId"].ToInt());

            gridView1.RefreshDataSource("UpdUnsped_Acilan2", esle.Serialize());
            gridView1.BestFitColumns();
        }

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "UNSPED_DOSYA_NO")
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }
    }
}
